/* Name: ViewControl
 * Author: John Gideon
 * Date: 5-8-12
 * Description: Sets the current view of the room and responds to key presses by moving the view around
 * Revisions: See Subversion logs at http://code.google.com/p/software-engineering-2012/source/list
 */

import java.awt.Graphics2D;

public class ViewControl {
	
	private static int x;
	private static int y;
	private static Boolean track;
	private static Boolean centerOnEarth = false;
	
	// Sets the initial view of the room and determines if it should track key strokes
	public static void init(Boolean ptrack) {
		track = ptrack;
		if (!track) {
			x = 0;
			y = 0;
		}
	}
	
	// Called every frame: Move the view if tracking with key presses and center on Earth if the appropriate flag is set
	public static void update() {
		if (!Game.isPaused) {
			final int MOUSE_SPEED = 10; 
			if (track) {
				if (KeyInputHandler.isLeftPressed()) {
					x -= MOUSE_SPEED;
					if (x < 0) {x = 0;}
				}
				if (KeyInputHandler.isRightPressed()) {
					x += MOUSE_SPEED;
					if (x >= GameScreen.getWidth() - Game.screenWidth) {x = GameScreen.getWidth()-Game.screenWidth-1;}
				}
				if (KeyInputHandler.isUpPressed()) {
					y -= MOUSE_SPEED;
					if (y < 0) {y = 0;}
				}
				if (KeyInputHandler.isDownPressed()) {
					y += MOUSE_SPEED;
					if (y >= GameScreen.getHeight() - Game.screenHeight) {y = GameScreen.getHeight()-Game.screenHeight-1;}
				}
			} else {
				x = 0;
				y = 0;
			}
			if (centerOnEarth) {
				Planet pid = GameScreen.getEarth();
				centerOn(pid.x,pid.y);
				centerOnEarth = false;
			}
		}
	}
	
	// Translate the view to enable drawing within the view
	public static void startView() {
		if (track) {
			Graphics2D gh = Game.getGraphicsHandle();
			gh.translate(-x, -y);
		}
	}
	
	// Translate back to enable drawing relative to the screen
	public static void stopView() {
		if (track) {
			Graphics2D gh = Game.getGraphicsHandle();
			gh.translate(x, y);
		}
	}
	
	// Return the x position of the mouse within the view
	public static int mouseX() {
		if (Mouse.getX() == 999999) {
			return 999999;
		} else {
			return x+Mouse.getX();
		}
	}
	
	// Return the y position of the mouse within the view
	public static int mouseY() {
		if (Mouse.getY() == 999999) {
			return 999999;
		} else {
			return y+Mouse.getY();
		}
	}
	
	// Used to set the x position of the view within the bounds of the room
	public static void setX(int px) {
		if (!Game.isPaused) {
			x = px;
			if (x < 0) {x = 0;}
			if (x >= GameScreen.getWidth() - Game.screenWidth) {x = GameScreen.getWidth()-Game.screenWidth-1;}
		}
	}
	
	// Used to set the y position of the view within the bounds of the room
	public static void setY(int py) {
		if (!Game.isPaused) {
			y = py;
			if (y < 0) {y = 0;}
			if (y >= GameScreen.getHeight() - Game.screenHeight) {y = GameScreen.getHeight()-Game.screenHeight-1;}
		}
	}
	
	// Used to center the position of the view within the bounds of the room
	public static void centerOn(int px, int py) {
		if (!Game.isPaused) {
			setX(px - Game.screenWidth/2);
			setY(py - Game.screenHeight/2);
		}
	}
	
	public static void centerToEarth() {centerOnEarth = true;}
	public static int getX() {return x;}
	public static int getY() {return y;}
}
